/**  
* @Title: ShopController.java  
* @Description: TODO(用一句话描述该文件做什么)  
* @author 张鑫
* @phone 17561783855  
* @date 2019年4月1日  
*/
package com.fortune.rainshine.service.controller;

import com.fortune.rainshine.common.BaseController;
import com.fortune.rainshine.common.service.CityService;
import com.fortune.rainshine.common.utils.StringUtil;
import com.fortune.rainshine.service.service.ShopService;
import com.fortune.rainshine.system.service.RoleService;
import com.fortune.rainshine.system.service.UserService;
import com.fortune.rainshinecommon.base.CityModel;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.base.ParmsModel;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.model.ShopModel;
import com.fortune.rainshinecommon.model.system.Role;
import com.fortune.rainshinecommon.model.system.User;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @ClassName: ShopController
 * @Description: TODO(门店维护)
 * @author 张鑫
 * @phone 17561783855
 * @date 2019年4月1日
 * 
 */
@Controller
@RequestMapping("/service/shop")
public class ShopController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(ShopController.class);

	@Autowired
    private ShopService service;
	@Autowired
    private CityService cityService;
	@Autowired
    private UserService userService;
	@Autowired
	private RoleService roleService;

	@RequiresPermissions("shop:view")
	@RequestMapping()
	public String shop(Model model) {
		List<Role> roles = roleService.list(false);
		model.addAttribute("roles", roles);
		// 获取用户权限
		Set<String> authorities = getLoginUserAuthorities();
		// 获取登录用户的按钮展示情况
		model.addAttribute("add", authorities.contains("shop:add"));
		model.addAttribute("edit", authorities.contains("shop:edit"));
		model.addAttribute("delete", authorities.contains("shop:delete"));

		return "shop/shopList.html";
	}

	@RequestMapping("/editForm")
	public String addUser(String shop,Model model) {
		try {
			Object citys = null;
			Object areas = null;
			if(StringUtil.isNotBlank(shop) && shop != "null") {
				Gson gson = new GsonBuilder().setDateFormat("yyyy/MM/dd HH:mm:ss").create();  
				ShopModel shopModel = gson.fromJson(shop, ShopModel.class);
				if(!shopModel.getCityCode().isEmpty()) {
					CityModel city = cityService.getCity(shopModel.getCityCode());
					citys = cityService.getAreaList(city).getData();
				}
				if(!shopModel.getAreaCode().isEmpty()) {
					CityModel area = cityService.getCity(shopModel.getAreaCode());
					areas = cityService.getAreaList(area).getData();
				}
			}
			model.addAttribute("citys", citys);
			model.addAttribute("areas", areas);
			model.addAttribute("provices", cityService.getAreaList(new CityModel()).getData());
			List<User>  managers = userService.getUsersByRole("manager");//获取所有店长
			List<User>  merchants = userService.getUsersByRole("merchant");//获取所有招商
			model.addAttribute("managers", managers);
			model.addAttribute("merchants", merchants);
		} catch (Exception e) {
			logger.error("editForm ====> {}",e);
		}
		
		return "shop/shop_form.html";
	}
	
	@RequiresPermissions("shop:add")
    @ResponseBody
    @RequestMapping("/add")
    public MsgModel add(@RequestBody ShopModel model) {
		try {
			model.setUser(this.getLoginUser());
			model.setShopLevel(3);
	    	return service.add(model);
		} catch (Exception e) {
			logger.error("add ====> {}",e);
			return new MsgModel(ResultCode.FAILURE);
		}
    }
	
	/**
     * @Title: list
     * @Description: 列表
     * @author zhangxin
     * @date 2019年3月26日
     */
    @RequiresPermissions("shop:view")
    @ResponseBody
    @GetMapping("/list")
    public MsgModel list(Integer page, Integer limit, String searchKey, String searchValue,Integer shopId) {
    	try {
            	page = (page -1)*limit;
            	limit = limit+page;
            if (StringUtil.isBlank(searchValue)) {
                searchKey = null;
            }
           
            ParmsModel parm = new ParmsModel(page,limit,searchKey,searchValue);
            //获取商铺id
//            parm.setType(shopId);
            parm.setShopId(shopId);
			return service.list(parm);
		} catch (Exception e) {
			logger.error("list ====> {}",e);
			return new MsgModel(ResultCode.FAILURE);
		}
    }
    
    @RequiresPermissions("shop:edit")
    @ResponseBody
    @RequestMapping("/update")
    public MsgModel update(@RequestBody ShopModel model) {
    	try {
    		model.setUser(this.getLoginUser());
            return service.update(model);
		} catch (Exception e) {
			logger.error("update ====> {}",e);
			return new MsgModel(ResultCode.FAILURE);
		}
    }
    
    /**
     * @Title: delete
     * @Description: 删除
     * @author zhangxin
     * @phone 17561783855
     * @date 2019年3月28日
     */
    @RequiresPermissions("shop:delete")
    @ResponseBody
    @RequestMapping("/delete")
    public MsgModel delete(@RequestBody ShopModel model) {
    	try {
    		return service.delete(model);
		} catch (Exception e) {
			logger.error("delete ====> {}",e);
			return new MsgModel(ResultCode.FAILURE);
		}
    }
}
